package top.infopub.mgr.bpa.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.enums.bpa.BpaProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.CotBackupDao;
import top.infopub.mgr.bpa.domain.CotBackupDomain;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.CotBackupService;
import top.infopub.mgr.bpa.vo.CotBackupVo;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.model.bpa.BpaCotBackup;
import top.infopub.model.bpa.CotBackup;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;


@Service
public class CotBackupServiceImpl extends GenericServiceImpl<BpaCotBackup, String> implements CotBackupService {

    @Autowired
    private CotBackupDao cotBackupDao;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;


    @Override
    public GenericDao<BpaCotBackup, String> getDao() {
        return cotBackupDao;
    }

    @Override
    public Page<CotBackupDomain> selectPageByDomainWhere(Page<CotBackupDomain> page,
                                                         CotBackupVo cotBackupVo) throws ParseException {
        //处理时间范围
        if (!StringUtils.isEmpty(cotBackupVo.getDates()))
        {
            String[] splitTime = cotBackupVo.getDates().split("~");
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            cotBackupVo.setStartDate(format.parse(splitTime[0]));
            cotBackupVo.setEndDate(format.parse(splitTime[1]));
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<CotBackupDomain>)cotBackupDao.selectByDomainWhere(cotBackupVo);
    }

    @Override
    public CotBackup queryById(String id) {
        CotBackup cotBackup = cotBackupDao.queryById(id);
        //附件信息
        List<ProjectAttachment> attachmentList = projectAttachmentDao.queryByWhere(cotBackup.getProCode(), ProjectFileTypeEnum.FILE_TYPE75.getCode(), null);
        cotBackup.setFileAtt(fileAttaListToArray(attachmentList));
        return cotBackup;
    }

    @Override
    public RspVo saveAll(CotBackup cotBackup, List<ProjectAttachment> attachmentList, String bodyStr, AuthorizationVO userDtl, Integer submitType) {

        //处理主表数据
        if (StringUtils.isNotBlank(cotBackup.getId())){ //编辑
            cotBackupDao.updateCot(cotBackup);
        }else{//新增
            cotBackupDao.insertCot(cotBackup);
        }
        //处理附件
        if (attachmentList!=null && attachmentList.size()>0){
            //批量插入附件信息
            projectAttachmentDao.deleteByWhere(cotBackup.getProCode(), ProjectFileTypeEnum.FILE_TYPE75.getCode(), null);
            for (ProjectAttachment attachment : attachmentList) {
                attachment.setProCode(cotBackup.getProCode());
            }
            projectAttachmentDao.insertBatch(attachmentList);
        }
        if (submitType==2){
            bpaProcessManageService.sendProcess(BpaProcessDetailTypeEnum.PRO_NODE_TYPE5, bodyStr, cotBackup.getId(), cotBackup.getProCode(), userDtl);
        }
        return RspVo.getSuccessResponse();
    }

    @Override
    public CotBackup selectBySguid(String id) {
        return cotBackupDao.queryById(id);
    }

    public String[] fileAttaListToArray(List<ProjectAttachment> list) {
        if (list == null || list.size() <= 0) return null;
        String[] attaArr = new String[list.size()];
        int index = 0;
        for (ProjectAttachment pa : list) {
            StringBuffer tmp = new StringBuffer();
            tmp.append(pa.getSubTypes());
            tmp.append("|");
            tmp.append(pa.getFileName());
            tmp.append(pa.getFileSuffix());
            tmp.append("|");
            tmp.append(IdFactory.getUUIDSerialNumber());
            tmp.append("|");
            tmp.append(pa.getFilePath());
            attaArr[index] = tmp.toString();
            index++;
        }
        return attaArr;
    }
}
